package com.abc.robot01.activity.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandIn
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.shrinkOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


//https://github.com/android/compose-samples
//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/TabSamples.kt
//
//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/SwipeToDismissSamples.kt

// https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/DatePickerSamples.kt


//https://developer.android.com/develop/ui/compose/lists#item-animations



//https://stackoverflow.com/questions/78137347/jetpack-compose-lazy-column-swipe-to-delete-animation-does-not-end-smoothly




class EnterTransitionSamples : ComponentActivity() {

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun  LazyColumn05() {

        // List of items with a MutableStateList
        val items = remember { mutableStateListOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5") }

        // Function to remove an item with animation
        fun removeItem(index: String) {
                items.remove(index)
        }

        LazyColumn(modifier = Modifier
            .animateContentSize()
            .height(200.dp)
            .background(Color(0xFFff7788)),
            contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
            verticalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            items(items) { item ->
                var visible by remember { mutableStateOf(true) }
                val animatedHeight by animateDpAsState(targetValue = if (visible) 50.dp else 0.dp)
                Box(modifier = Modifier.animateItemPlacement(tween(durationMillis = 2000)).height(animatedHeight)) {
                    Text(
                        text = item,
                        style = MaterialTheme.typography.bodyLarge,
                        modifier = Modifier
                            .padding(vertical = 8.dp)
                            .clickable {
                                visible =false
//                                removeItem(item)
                                Log.e(TAG, "remove $item");
                            }
                    )
//                }
            }
            }
        }
    }


    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun AnimatedItemRemovalDemo() {
        // List of items with a MutableStateList
        val items = remember { mutableStateListOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5") }

        // Function to remove an item with animation
        fun removeItem(index: Int) {
            if (index in items.indices) {
                items.removeAt(index)
            }
        }

        // LazyColumn with animated item removal
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            itemsIndexed(items, key = { index, item -> item }) { index, item ->
                var visible by remember { mutableStateOf(true) }

                val animatedHeight by animateDpAsState(targetValue = if (visible) 50.dp else 0.dp)
//                if (animatedHeight > 0.dp) {
                    Surface(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(animatedHeight)
                            .animateItemPlacement()
                            .background(Color.Red)
                            .clickable {
//                                visible = false
                                removeItem(index)
                            },
                        contentColor = Color.White
                    ) {
                        Box(
                            contentAlignment = Alignment.Center,
                            modifier = Modifier.fillMaxSize()
                        ) {
                            Text(text = item, fontSize = 18.sp)
                        }
                    }
//                }
            }
        }
    }


    @Composable
    private fun AnimatedVisibilityWithEnterAndExit() {
        // [START android_compose_animations_animated_visibility_enter_exit]
        var visible by remember { mutableStateOf(true) }

        Column {
             Button(onClick = { visible =visible.not()}) {
                  Text(text = "Change")
             }

            val density = LocalDensity.current
            AnimatedVisibility(
                visible = visible,
                enter = slideInVertically {
                    // Slide in from 40 dp from the top.
                    with(density) { 40.dp.roundToPx() }
                },
                exit = fadeOut()
//                        + expandVertically(
//                    // Expand from the top.
//                    expandFrom = Alignment.Top
//                ) + fadeIn(
//                    // Fade in with the initial alpha of 0.3f.
//                    initialAlpha = 0.3f
//                )

                ,

//                exit = slideOutVertically() + shrinkVertically() + fadeOut()
            ) {
                Text(
                    "Hello",
                    Modifier
                        .fillMaxWidth()
                        .height(200.dp)
                )
            }
        }
        // [END android_compose_animations_animated_visibility_enter_exit]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("FirstFragment","MainActivity  onCreate");
        setContent {
            MaterialTheme {
                Surface() {
//                    AnimatedVisibilityWithEnterAndExit()
//                    LazyColumn05()

//                    AnimatedVisibilityCookbook()
//                    AnimatedVisibilityCookbook2()
//                    AnimatedVisibilityCookbook3()
//                    AnimatedVisibilityCookbook4()
                    AnimatedVisibilityCookbook5()
//                    AnimatedItemRemovalDemo()
//                    BasicReorderingDemo()
//                    AddNewItemDemo()
                }
            }
        }
        }

    companion object{
     val  TAG="LazyColumnSamples"
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun BasicReorderingDemo() {
    var items by remember { mutableStateOf((1..5).map { "Item $it" }.toMutableList()) }

    LazyColumn {
        itemsIndexed(items) { index, item ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(8.dp)
                    .animateItemPlacement(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(text = item)
                Row {
                    Button(onClick = {
                        if (index > 0) {
                            items = items.toMutableList().apply {
                                add(index - 1, removeAt(index))
                            }
                        }
                    }) { Text("Up") }
                    Button(onClick = {
                        if (index < items.size - 1) {
                            items = items.toMutableList().apply {
                                add(index + 1, removeAt(index))
                            }
                        }
                    }) { Text("Down") }
                }
            }
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun AddNewItemDemo() {
    var items by remember { mutableStateOf((1..5).map { "Item $it" }.toMutableList()) }

    Column {
        Button(onClick = {
            items = items.toMutableList().apply {
                add(0, "New Item ${items.size + 1}")
            }
        }) {
            Text("Add Item")
        }

        LazyColumn {
            itemsIndexed(items) { _, item ->
                Text(
                    text = item,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                        .animateItemPlacement()
                )
            }
        }
    }
}

@Composable
fun AnimatedVisibilityCookbook() {
    Box(modifier = Modifier.fillMaxSize()) {
        // [START android_compose_animation_cookbook_visibility]
        var visible by remember {
            mutableStateOf(true)
        }
        // Animated visibility will eventually remove the item from the composition once the animation has finished.
        AnimatedVisibility(visible, enter = slideInVertically(), exit = slideOutVertically(){-it}) {
            // your composable here
            // [START_EXCLUDE]
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Green)
            ) {
            }
            // [END_EXCLUDE]
        }
        // [END android_compose_animation_cookbook_visibility]
        Button(modifier = Modifier.align(Alignment.BottomCenter), onClick = {
            visible = !visible
        }) {
            Text("Show/Hide")
        }
    }
}


@Composable
fun AnimatedVisibilityCookbook2() {
    Box(modifier = Modifier.fillMaxSize()) {
        // [START android_compose_animation_cookbook_visibility]
        var visible by remember {
            mutableStateOf(true)
        }
        // Animated visibility will eventually remove the item from the composition once the animation has finished.
        AnimatedVisibility(visible, enter = slideInHorizontally(){-it}, exit = slideOutHorizontally{-it}) {
            // your composable here
            // [START_EXCLUDE]
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Green)
            ) {
            }
            // [END_EXCLUDE]
        }
        // [END android_compose_animation_cookbook_visibility]
        Button(modifier = Modifier.align(Alignment.BottomCenter), onClick = {
            visible = !visible
        }) {
            Text("Show/Hide")
        }
    }
}


@Composable
fun AnimatedVisibilityCookbook3() {
    Box(modifier = Modifier.fillMaxSize()) {
        // [START android_compose_animation_cookbook_visibility]
        var visible by remember {
            mutableStateOf(true)
        }
        // Animated visibility will eventually remove the item from the composition once the animation has finished.
        AnimatedVisibility(visible, enter = fadeIn(animationSpec = tween(durationMillis = 2000)), exit = fadeOut(animationSpec = tween(durationMillis = 2000))) {
            // your composable here
            // [START_EXCLUDE]
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Green)
            ) {
            }
            // [END_EXCLUDE]
        }
        // [END android_compose_animation_cookbook_visibility]
        Button(modifier = Modifier.align(Alignment.BottomCenter), onClick = {
            visible = !visible
        }) {
            Text("Show/Hide")
        }
    }
}


@Composable
fun AnimatedVisibilityCookbook4() {
    Box(modifier = Modifier.fillMaxSize()) {
        // [START android_compose_animation_cookbook_visibility]
        var visible by remember {
            mutableStateOf(true)
        }
        // Animated visibility will eventually remove the item from the composition once the animation has finished.
        AnimatedVisibility(visible, enter = scaleIn(initialScale = 0.0f),
            exit = scaleOut(targetScale = 0.0f)) {
            // your composable here
            // [START_EXCLUDE]
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Green)
            ) {
            }
            // [END_EXCLUDE]
        }
        // [END android_compose_animation_cookbook_visibility]
        Button(modifier = Modifier.align(Alignment.BottomCenter), onClick = {
            visible = !visible
        }) {
            Text("Show/Hide")
        }
    }
}

@Composable
fun AnimatedVisibilityCookbook5() {
    Box(modifier = Modifier.fillMaxSize()) {
        // [START android_compose_animation_cookbook_visibility]
        var visible by remember {
            mutableStateOf(true)
        }
        // Animated visibility will eventually remove the item from the composition once the animation has finished.
        AnimatedVisibility(visible,
            enter = expandIn( animationSpec= tween(durationMillis = 3000),
                // Expands from the top-left corner of the item
                expandFrom = Alignment.TopStart,
                // Optional, controls the initial offset from where expansion starts
                initialSize = { fullSize -> IntSize(fullSize.width / 100, fullSize.height / 100) }
            ),
            exit = shrinkOut(animationSpec= tween(durationMillis = 3000))

        ) {
            // your composable here
            // [START_EXCLUDE]
            Box(
                modifier = Modifier
                    .size(200.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Green)
            ) {
            }
            // [END_EXCLUDE]
        }
        // [END android_compose_animation_cookbook_visibility]
        Button(modifier = Modifier.align(Alignment.BottomCenter), onClick = {
            visible = !visible
        }) {
            Text("Show/Hide")
        }
    }
}











